﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_VBPQType_Edit
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not Request.QueryString("id").ToString.Equals("0") Then
                hidID.Value = Request.QueryString("id")
                ShowData()
            End If
        End If
    End Sub
    Protected Sub ShowData()
        Using data As New VSEntities
            Dim p As VBPQLoaiVanBan = (From a In data.VBPQLoaiVanBans Where a.ID = hidID.Value).SingleOrDefault
            If Not p Is Nothing Then
                txtSigner.Text = IIf(IsNothing(p.LoaiVanBan) = True, "", p.LoaiVanBan)
            End If
        End Using
    End Sub
#End Region
#Region "Event for control"
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            'Kiem tra tu khoa da ton tai chua
            Dim q = (From k In data.VBPQLoaiVanBans Where k.LoaiVanBan.ToUpper = txtSigner.Text.Trim.ToUpper And k.ID <> hidID.Value Select k).FirstOrDefault
            If q Is Nothing Then
                Dim p As VBPQLoaiVanBan = (From o In data.VBPQLoaiVanBans Where o.ID = hidID.Value).SingleOrDefault
                Dim sTitle As String = p.LoaiVanBan
                Try
                    p.LoaiVanBan = txtSigner.Text
                    data.SaveChanges()
                    RemoveKeyWordJavascript(sTitle)
                    AddKeyWordJavascript(txtSigner.Text)
                    AddKeyWordJavascript(makeURLFriendly(txtSigner.Text.Trim))
                    Insert_App_Log("Edit KeyWord:" & txtSigner.Text.Trim & "", Function_Name.KeyWord, Audit_Type.Edit, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                    Dim strMessage As String = ""
                    'strMessage &= "window.parent.setVBPQValue();"
                    strMessage &= "window.location ='../../Page/VBPQType/Detail.aspx?id=" & hidID.Value & "';"
                    Excute_Javascript(strMessage, Page, True)
                    'Excute_Javascript("Alertbox('Cập nhật chữ ký thành công.');window.location ='../../Page/VBPQSigner/Detail.aspx?id=" & hidID.Value & "';", Me.Page, True)
                Catch ex As Exception
                    log4net.Config.XmlConfigurator.Configure()
                    log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                    Excute_Javascript("Alertbox('Cập nhật loại văn bản thất bại.');", Me.Page, True)
                End Try
            Else
                showError.Visible = True
            End If
        End Using
    End Sub
    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region
End Class
